package com.neusoft.chanceserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.neusoft.chanceserver.dto.ChanceSearch;
import com.neusoft.chanceserver.dto.RespBean;
import com.neusoft.chanceserver.entity.*;
import com.neusoft.chanceserver.mapper.ChanceMapper;
import com.neusoft.chanceserver.mapper.ChanceSubchanceMapper;
import com.neusoft.chanceserver.mapper.CustomerChanceMapper;
import com.neusoft.chanceserver.mapper.SubchanceMapper;
import com.neusoft.chanceserver.service.IChanceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.bouncycastle.asn1.x509.qualified.BiometricData;
import org.bouncycastle.jcajce.provider.symmetric.AES;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jnlp.IntegrationService;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 主机会表 服务实现类
 * </p>
 *
 * @author liuxp
 * @since 2021-07-12
 */
@Service
public class ChanceServiceImpl extends ServiceImpl<ChanceMapper, Chance> implements IChanceService {
    @Autowired
    ChanceMapper chanceMapper;

    @Autowired
    SubchanceMapper subchanceMapper;

    @Autowired
    ChanceSubchanceMapper chanceSubchanceMapper;

    @Autowired
    CustomerChanceMapper customerChanceMapper;

    @Override
    public RespBean searchChance(ChanceSearch chanceSearch) {
        LambdaQueryWrapper<Chance> chanceLambdaQueryWrapper = Wrappers.lambdaQuery();
        if(hasValue(chanceSearch.getChanceProduct() )){
            LambdaQueryWrapper<Subchance> subchanceQueryWrapper = Wrappers.lambdaQuery();
            subchanceQueryWrapper.eq(Subchance::getSubchanceProduct, chanceSearch.getChanceProduct());
            List<Subchance> subchances = subchanceMapper.selectList(subchanceQueryWrapper);
            if(subchances.size() >= 1){
                List<Integer> subchanceId = new ArrayList<>();
                for (Subchance subchance : subchances) {
                    subchanceId.add(subchance.getSubchanceId());
                }
                LambdaQueryWrapper<ChanceSubchance> chanceSubchanceLambdaQueryWrapper = Wrappers.lambdaQuery();
                chanceSubchanceLambdaQueryWrapper.in(ChanceSubchance::getSubchanceId, subchanceId);
                List<ChanceSubchance> chanceSubchances = chanceSubchanceMapper.selectList(chanceSubchanceLambdaQueryWrapper);
                if(chanceSubchances.size() > 0){
                    List<Integer> chanceIds = new ArrayList<>();
                    for (ChanceSubchance chanceSubchance : chanceSubchances) {
                        chanceIds.add(chanceSubchance.getChanceId());
                    }
                    chanceLambdaQueryWrapper.in(Chance::getChanceId, chanceIds);
                }
            }
        }


        if (hasValue(chanceSearch.getChanceBelong() )) {
            chanceLambdaQueryWrapper.eq(Chance::getChanceBelong, chanceSearch.getChanceBelong());
        }
        if (hasValue(chanceSearch.getChanceCode())) {
            chanceLambdaQueryWrapper.eq(Chance::getChanceCode, chanceSearch.getChanceCode());
        }
        if (hasValue(chanceSearch.getChanceDept() )) {
            chanceLambdaQueryWrapper.eq(Chance::getCustomerMgrDept, chanceSearch.getChanceDept());
        }
        if (hasValue(chanceSearch.getChanceMgr() )) {
            chanceLambdaQueryWrapper.eq(Chance::getCustomerMgrName, chanceSearch.getChanceMgr());
        }
        if (hasValue(chanceSearch.getChanceName() )) {
            chanceLambdaQueryWrapper.eq(Chance::getChanceName, chanceSearch.getChanceName());
        }
        if (hasValue(chanceSearch.getChancePhase() )) {
            chanceLambdaQueryWrapper.eq(Chance::getChancePhase, chanceSearch.getChancePhase());
        }
        if (hasValue(chanceSearch.getChanceStatus() )) {
            chanceLambdaQueryWrapper.eq(Chance::getChanceStatus, chanceSearch.getChanceStatus());
        }
        if (hasValue(chanceSearch.getChanceType() )) {
            chanceLambdaQueryWrapper.eq(Chance::getChanceType, chanceSearch.getChanceType());
        }
        if (hasValue(chanceSearch.getChanceTimeFrom() )) {
            chanceLambdaQueryWrapper.ge(Chance::getChanceTime, chanceSearch.getChanceTimeFrom());
        }
        if (hasValue(chanceSearch.getChanceTimeTo() )) {
            chanceLambdaQueryWrapper.le(Chance::getChanceTime, chanceSearch.getChanceTimeTo());
        }
        List<Chance> chances = chanceMapper.selectList(chanceLambdaQueryWrapper);
        RespBean respBean = RespBean.ok(200, "Find " + chances.size() + " chance!");
        respBean.setObj(chances);
        return respBean;
    }

    @Override
    public RespBean addChance(Chance chance) {
        RespBean respBean = RespBean.ok(200, "Chance added successfully!");
        chanceMapper.insert(chance);
        Integer customerId = Integer.parseInt(chance.getCustomerName());
        Integer chanceId = chance.getChanceId();
        CustomerChance customerChance = new CustomerChance();
        customerChance.setChanceId(chanceId);
        customerChance.setCustomerId(customerId);
        customerChanceMapper.insert(customerChance);
        respBean.setObj(chance.getChanceId());
        return respBean;
    }

    @Override
    public RespBean deleteChance(int id) {
        RespBean respBean = RespBean.ok(200, "Chance deleted successfully!");
        chanceMapper.deleteById(id);
        return respBean;
    }

    @Override
    public RespBean updateChance(Chance chance) {
        RespBean respBean = RespBean.ok(200, "Chance updated successfully!");
        chanceMapper.updateById(chance);
        return respBean;
    }

    @Override
    public RespBean queryAllChance() {
        List<Chance> chanceList = chanceMapper.selectList(null);
        if (chanceList.size() < 1){
            return RespBean.error(500,"No result",null);
        }else {
            return RespBean.ok(200,"Search success!",chanceList);
        }
    }

    @Override
    public RespBean getChanceByCustomer(Customer customer) {
        RespBean respBean =RespBean.ok(200, "Chance found successfully!");
        Integer customerId = customer.getCustomerId();
        LambdaQueryWrapper<CustomerChance> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CustomerChance::getCustomerId, customerId);
        List<CustomerChance> customerChances = customerChanceMapper.selectList(wrapper);
        List<Chance> chances = new ArrayList<>();
        if(customerChances.size() >= 1){
            List<Integer> chanceIds = new ArrayList<>();
            for(CustomerChance customerChance : customerChances){
                chanceIds.add(customerChance.getChanceId());
            }
            chances = chanceMapper.selectBatchIds(chanceIds);
            respBean.setObj(chances);
        }
        return respBean;
    }

    private Boolean hasValue(String s){
        return s != null && s.length() > 0;
    }



}
